package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.ProductStockMapper;
import cn.wolfcode.crm.mapper.StockincomeBillItemMapper;
import cn.wolfcode.crm.mapper.StockincomeBillMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IStockincomeBillService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class StockincomeBillServiceImpl implements IStockincomeBillService {
    @Autowired
    private StockincomeBillMapper mapper;
    @Autowired
    private StockincomeBillItemMapper itemMapper;
    @Autowired
    private ProductStockMapper productStockMapper;

    public void save(StockincomeBill entity) {
        //把录入时间 设置进去
        entity.setInputTime(new Date());
        Employee employee = new Employee();
        employee.setId(1L);
        entity.setInputUser(employee);
        entity.setState(StockincomeBill.STATUS_QUIT);
        //设置状态
        BigDecimal totalAmount = BigDecimal.ZERO; // 总金额
        BigDecimal totalNumber = BigDecimal.ZERO; // 总数量
        List<StockincomeBillItem> items = entity.getItems();
        for (StockincomeBillItem item : items) {
            // 计算总金额和总数量

            totalAmount = totalAmount.add(item.getCostPrice().multiply(item.getNumber())); // 总金额
            totalNumber = totalNumber.add(item.getNumber());
        }
        entity.setTotalAmount(totalAmount);
        entity.setTotalNumber(totalNumber);
        mapper.insert(entity);
        for (StockincomeBillItem item : items) {

            item.setBillId(entity.getId()); // 入库单Id
            item.setAmount(item.getCostPrice().multiply(item.getNumber()));
            // 然后保存到数据库里面去
            itemMapper.insert(item);

        }
    }

    public void update(StockincomeBill entity) {
        StockincomeBill bill = mapper.selectByPrimaryKey(entity.getId());
        if (bill.getState() == StockincomeBill.STATUS_QUIT) {
            //获取页面传输过来的数据
            List<StockincomeBillItem> items = entity.getItems();
            //先删除老的明细,在添加新的明细
            itemMapper.deleteByBillId(entity.getId());
            BigDecimal totalAmount = BigDecimal.ZERO; // 总金额
            BigDecimal totalNumber = BigDecimal.ZERO; // 总数量
            for (StockincomeBillItem item : items) {
                // 计算总金额和总数量

                totalAmount = totalAmount.add(item.getCostPrice().multiply(item.getNumber())); // 总金额
                totalNumber = totalNumber.add(item.getNumber());
            }
            for (StockincomeBillItem item : items) {

                item.setBillId(entity.getId()); // 入库单Id
                item.setAmount(item.getCostPrice().multiply(item.getNumber()));
                // 然后保存到数据库里面去
                itemMapper.insert(item);
            }
            entity.setTotalAmount(totalAmount);
            entity.setTotalNumber(totalNumber);
            mapper.updateByPrimaryKey(entity);
        }

    }

    public void delete(Long id) {
        itemMapper.deleteByBillId(id);
        mapper.deleteByPrimaryKey(id);
    }

    public List<StockincomeBill> list() {
        return mapper.selectAll();
    }

    public StockincomeBill get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public PageResult query(QueryObject qo) {
        return PageUtils.query(qo, mapper);
    }


    public void changeAudit(Long id) {
        StockincomeBill bill = mapper.selectByPrimaryKey(id);
        if (bill.getState() == StockincomeBill.STATUS_QUIT) {
            //设置审核人,审核时间,审核状态
            Employee username = new Employee();
            username.setId(1L);
            bill.setAuditor(username);
            bill.setAuditTime(new Date());
            bill.setState(OrderBill.STATUS_EXIST);
            mapper.changeAudit(bill);
        }
        List<StockincomeBillItem> items = bill.getItems();
        //修改库存量
        for (StockincomeBillItem item : items) {
            ProductStock ps = productStockMapper.selectByProductAndStockId(item.getProduct().getId(),
                    bill.getDepot().getId());
            if (ps == null) {
                ps = new ProductStock();
                ps.setDepot(bill.getDepot());
                ps.setPrice(item.getCostPrice());
                ps.setStoreNumber(item.getNumber());
                ps.setAmount(item.getCostPrice().multiply(item.getNumber()));
                ps.setProduct(item.getProduct());
                productStockMapper.insert(ps);
            } else {
                ps.setAmount(ps.getAmount().add(item.getAmount()));
                ps.setStoreNumber(ps.getStoreNumber().add(item.getNumber()));

                BigDecimal price = ps.getAmount().divide(ps.getStoreNumber(), 2, BigDecimal.ROUND_HALF_UP);//保留小数位数
                ps.setPrice(price);
                productStockMapper.updateByPrimaryKey(ps);
            }
        }

    }

}
